package com.lslk.othello.ai;

import android.util.Log;
/**
 * AI Class
 * @author Edison Wang
 * To View the logs for this class :
 * adb logcat Agent:I *:S
 */
public class Agent {
	public final static String TAG="Agent";	
	static final int MIN=-9999999;
	static final int MAX=+9999999;
	/**
	 * legal[0] = num;
	 * legal[1->n-1]=legal move locations stored
	 */
	public static int self;
	public static long end;
	public static int last_best_value;
	public static long last_time_used;
	public static int last_depth;
	//public static int[] dynamic_state; - could be used.
	// when found the best, perform the move and then store it
	// when next round is possible, perform the move.
	public synchronized static int getNextMove(int[] pieces, boolean blackTurn, int time, int current_steps){
		time=(int) (time*0.95);
		long start=System.currentTimeMillis();
		long end=System.currentTimeMillis()+time;
		Agent.end=end;
		int depth=1;
		int piece=0;
		self=blackTurn?2:1;
		boolean isEndGame=(current_steps>48);
		//Iterative Deepening 
		while(System.currentTimeMillis()<=end && depth<30){
			piece=minimax(depth,pieces,self,isEndGame);
			depth++;
			if(Math.abs(Agent.last_best_value)>=AgentValues.goalVal || Agent.last_best_value==-1)
				break;
		}
		last_time_used=System.currentTimeMillis()-start;
		last_depth=depth-1;
		//Log.i(TAG,"AI Found: "+GameBoard.mNames[piece]+" ("+last_best_value+") time used: "+last_time_used);
		return piece;
	}
	private static int minimax(int cutoff,int[] pieces,int turn,boolean inEndGame){
		Log.i(TAG,"Start Minimax search with depth: "+cutoff);
		int[] moves=findLegalMoves(pieces,turn);
		int best_value=MIN;
		int best_move=moves[1];
		int temp=0;
		int other=turn==2?1:2;
		if(moves[0]==2){//One move.
			last_best_value=-1;
			return best_move;
		}
		for(int i=1;i<moves[0];i++){
			Log.i(TAG,"Minimax Prcess Move: "+GameBoard.mNames[moves[i]]);
			temp=-search(cutoff-1,1,GameBoard.resultFrom(pieces,moves[i],turn),other,turn,0,MIN,MAX,moves[i],inEndGame);
			Log.i(TAG,"Minimax: "+GameBoard.mNames[moves[i]]+" ("+temp+") @"+i+" ");
			if(best_value<temp){
				best_value=temp;
				best_move=moves[i];
			}
			if(Math.abs(best_value)>=AgentValues.goalVal)
				break;
		}
		last_best_value=best_value;
		Log.i(TAG,"Minimax Move Found: "+GameBoard.mNames[best_move]+ " ("+best_value+")");
		return best_move;
	}
	/**
	 * Depth Limited Minimax Search
	 * State:
	 * 	@param doMax - if true do Max else do Min
	 * 	@param cutoff - Cut off Depth
	 * 	@param depth  - Current Depth - use for potential dynamic programming.
	 * 	@param cBest
	 * 	@param pieces 
	 * 	@param turn
	 *  @param skipped
	 * @return utility
	 */ 
	private static int search(int cutoff, int depth, int[] pieces, int turn,int other,int skipped,int a,int b,int move,boolean isEndGame) {
		//Goal Test
		int state=GameBoard.isOver(pieces, skipped);
		int[] moves=findLegalMoves(pieces,turn);
		//Log.i(TAG,"Legal moves found depth "+depth+Arrays.)
		if(cutoff==0 || state!=0 || (System.currentTimeMillis()>end)) {
			//Log.i(TAG,"H="+Agent.h1(pieces,state,moves,turn)+ " depth-"+ depth);
			if(isEndGame)
				return Agent.e2(pieces,state,moves,turn,other);//White End
			else
				return Agent.h4(pieces,state,moves,turn,other);//White First
			
			/*
			if(self==1)
				if(isEndGame)
					return Agent.e2(pieces,state,moves,turn,other);//White End
				else
					return Agent.h4(pieces,state,moves,turn,other);//White First
			else
				if(isEndGame)
					return Agent.e1(pieces,state,moves,turn,other);//White End
				else
					return Agent.h1(pieces,state,moves,turn,other);//White First
			*/
		}
		//Log.i(TAG,"alpha="+ a + " beta=" + b +" depth-"+ depth+" for "+(move!=-1?GameBoard.mNames[move]: "-1"));
		//Check if there are moves left
		if(moves[0]>1){
			//Alpha-beta pruning for the rest
			int value=0;
			for(int i=1;i<moves[0];i++){
				value=-Agent.search(cutoff-1,depth+1,GameBoard.resultFrom(pieces,moves[i],turn),other,turn, skipped,-b,-a,moves[i],isEndGame);
				Log.i(TAG,"Depth "+depth+" "+GameBoard.mNames[moves[i]]+" ("+value+")");
				a=Math.max(a,value);
				if(a>=b){
					//Log.i(TAG,"Returned @1 "+a+ " depth-"+ depth);
					return a;
				}
				
			}
			//Log.i(TAG,"Returned @2 "+a+ " depth-"+ depth);
			return a;
		}
		//Log.i(TAG,"Returned @ c");
		a = Math.max(a,-Agent.search(cutoff-1,depth+1,pieces.clone(),other,turn, skipped+1,-b,-a,-1,isEndGame));//lastValue-Agent.skipVal;
		return a;
	}
	public static int[] findLegalMoves(int[] pieces,int turn){
		int[] legal=new int[64];
		int n=1;
		for(int i=0;i<64;i++)
			if(GameBoard.isLegal(pieces,i,turn)){
				legal[n]=i;
				n++;
			}
		legal[0]=n;
		return legal;
	}
	public static int h4(int[] pieces, int state, int[] moves,int turn,int other){
		if(state>1)
			if(state==turn+1)
				return AgentValues.goalVal;
			else
				return -AgentValues.goalVal;
		int numSelf=0;
		int i;
		int total=0;
		int score=0;
		//Evaluate Corners.
		if(pieces[0]>0){
			total++;
			if(pieces[0]==turn){
				numSelf++;
				score+=AgentValues.cornerVal;
			}else{
				score-=2*AgentValues.cornerVal;
			}
		}else{
			if(pieces[1]>0){
				total++;
				if(pieces[1]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
			if(pieces[8]>0){
				total++;
				if(pieces[8]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}		
			if(pieces[9]>0){
				total++;
				if(pieces[9]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
		}
		if(pieces[7]>0){
			total++;
			if(pieces[7]==turn){
				score+=AgentValues.cornerVal;
			}else{
				score-=2*AgentValues.cornerVal;
			}
		}else{
			if(pieces[6]>0){
				total++;
				if(pieces[6]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
			if(pieces[14]>0){
				total++;
				if(pieces[14]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
			if(pieces[15]>0){
				total++;
				if(pieces[15]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
		}
		if(pieces[56]>0){
			total++;
			if(pieces[56]==turn){
				score+=AgentValues.cornerVal;
			}else{
				score-=2*AgentValues.cornerVal;
			}
		}else{
			if(pieces[48]>0){
				total++;
				if(pieces[48]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
			if(pieces[49]>0){
				total++;
				if(pieces[49]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
			if(pieces[57]>0){
				total++;
				if(pieces[57]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
		}
		if(pieces[63]>0){
			total++;
			if(pieces[63]==turn){
				score+=AgentValues.cornerVal;
			}else{
				score-=2*AgentValues.cornerVal;
			}
		}else{
			if(pieces[54]>0){
				total++;
				if(pieces[54]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
			if(pieces[55]>0){
				total++;
				if(pieces[55]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
			if(pieces[62]>0){
				total++;
				if(pieces[62]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
		}
		//Counting
		for(i=2;i<=5;i++){
			if(pieces[i]>0){
				total++;
				if(pieces[i]==turn)
					numSelf++;
			}
		}		
		for(i=10;i<13;i++){
			if(pieces[i]>0){
				total++;
				if(pieces[i]==turn)
					numSelf++;
			}
		}
		for(i=16;i<47;i++){
			if(pieces[i]>0){
				total++;
				if(pieces[i]==turn)
					numSelf++;
			}
		}
		for(i=50;i<53;i++){
			if(pieces[i]>0){
				total++;
				if(pieces[i]==turn)
					numSelf++;
			}
		}
		for(i=58;i<61;i++){
			if(pieces[i]>0){
				total++;
				if(pieces[i]==turn)
					numSelf++;
			}
		}
		Log.i(TAG,"Turn/Score/MoveVal/PieceVal "+turn+"/"+score+"/"+moves[0]*AgentValues.oppVal3+"/"+(2*numSelf-total)*AgentValues.pieceVal3);
		return (int) Math.round(score+moves[0]*AgentValues.oppVal3+(2*numSelf-total)*AgentValues.pieceVal3);
	}
	
	public static int e2(int[] pieces, int state, int[] moves,int turn,int other){
		if(state>1)
			if(state==turn+1)
				return AgentValues.goalVal;
			else
				return -AgentValues.goalVal;
		int numSelf=0;
		int i;
		int total=0;
		int score=0;
		//Evaluate Corners.
		if(pieces[0]>0){
			total++;
			if(pieces[0]==turn){
				numSelf++;
				score+=AgentValues.cornerVal;
			}else{
				score-=2*AgentValues.cornerVal;
			}
		}else{
			if(pieces[1]>0){
				total++;
				if(pieces[1]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
			if(pieces[8]>0){
				total++;
				if(pieces[8]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}		
			if(pieces[9]>0){
				total++;
				if(pieces[9]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
		}
		if(pieces[7]>0){
			total++;
			if(pieces[7]==turn){
				score+=AgentValues.cornerVal;
			}else{
				score-=2*AgentValues.cornerVal;
			}
		}else{
			if(pieces[6]>0){
				total++;
				if(pieces[6]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
			if(pieces[14]>0){
				total++;
				if(pieces[14]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
			if(pieces[15]>0){
				total++;
				if(pieces[15]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
		}
		if(pieces[56]>0){
			total++;
			if(pieces[56]==turn){
				score+=AgentValues.cornerVal;
			}else{
				score-=2*AgentValues.cornerVal;
			}
		}else{
			if(pieces[48]>0){
				total++;
				if(pieces[48]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
			if(pieces[49]>0){
				total++;
				if(pieces[49]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
			if(pieces[57]>0){
				total++;
				if(pieces[57]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
		}
		if(pieces[63]>0){
			total++;
			if(pieces[63]==turn){
				score+=AgentValues.cornerVal;
			}else{
				score-=2*AgentValues.cornerVal;
			}
		}else{
			if(pieces[54]>0){
				total++;
				if(pieces[54]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
			if(pieces[55]>0){
				total++;
				if(pieces[55]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
			if(pieces[62]>0){
				total++;
				if(pieces[62]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
		}
		//Counting
		for(i=2;i<=5;i++){
			if(pieces[i]>0){
				total++;
				if(pieces[i]==turn)
					numSelf++;
			}
		}		
		for(i=10;i<13;i++){
			if(pieces[i]>0){
				total++;
				if(pieces[i]==turn)
					numSelf++;
			}
		}
		for(i=16;i<47;i++){
			if(pieces[i]>0){
				total++;
				if(pieces[i]==turn)
					numSelf++;
			}
		}
		for(i=50;i<53;i++){
			if(pieces[i]>0){
				total++;
				if(pieces[i]==turn)
					numSelf++;
			}
		}
		for(i=58;i<61;i++){
			if(pieces[i]>0){
				total++;
				if(pieces[i]==turn)
					numSelf++;
			}
		}
		int reversibility=MIN;
		int bestMove=moves[0];
		int stability=MIN;
		int temp;
		int[] oppMoves=Agent.findLegalMoves(pieces, other);
		int opp=oppMoves[0];
		for(int j=1;j<moves[0];j++){
			temp=GameBoard.numTurned(pieces,moves[j],turn);
			if(temp>reversibility){
				bestMove=moves[j];
				reversibility=temp;
			}
		}
		int[] nextMove=Agent.findLegalMoves(GameBoard.resultFrom(pieces,bestMove,turn),other);
		for(int j=1;j<nextMove[0];j++)
			stability=Math.max(stability,GameBoard.numTurned(pieces,moves[j],other));
		return (int) Math.round(score+(moves[0]-opp)*AgentValues.oppVal3+(2*numSelf-total+reversibility-stability)*AgentValues.pieceVal3*1.5);
	}
	public static int h1(int[] pieces, int state, int[] moves,int turn,int other){
		if(state>1)
			if(state==turn+1)
				return AgentValues.goalVal;
			else
				return -AgentValues.goalVal;
		int numSelf=0;
		int i;
		int total=0;
		int score=0;
		//Evaluate Corners.
		if(pieces[0]>0){
			total++;
			if(pieces[0]==turn){
				numSelf++;
				score+=AgentValues.cornerVal;
			}else{
				score-=2*AgentValues.cornerVal;
			}
		}else{
			if(pieces[1]>0){
				total++;
				if(pieces[1]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
			if(pieces[8]>0){
				total++;
				if(pieces[8]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}		
			if(pieces[9]>0){
				total++;
				if(pieces[9]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
		}
		if(pieces[7]>0){
			total++;
			if(pieces[7]==turn){
				score+=AgentValues.cornerVal;
			}else{
				score-=2*AgentValues.cornerVal;
			}
		}else{
			if(pieces[6]>0){
				total++;
				if(pieces[6]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
			if(pieces[14]>0){
				total++;
				if(pieces[14]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
			if(pieces[15]>0){
				total++;
				if(pieces[15]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
		}
		if(pieces[56]>0){
			total++;
			if(pieces[63]==turn){
				score+=AgentValues.cornerVal;
			}else{
				score-=2*AgentValues.cornerVal;
			}
		}else{
			if(pieces[48]>0){
				total++;
				if(pieces[48]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
			if(pieces[49]>0){
				total++;
				if(pieces[49]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
			if(pieces[57]>0){
				total++;
				if(pieces[57]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
		}
		if(pieces[63]>0){
			total++;
			if(pieces[63]==turn){
				score+=AgentValues.cornerVal;
			}else{
				score-=2*AgentValues.cornerVal;
			}
		}else{
			if(pieces[54]>0){
				total++;
				if(pieces[54]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
			if(pieces[55]>0){
				total++;
				if(pieces[55]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
			if(pieces[62]>0){
				total++;
				if(pieces[62]==turn){
					score-=AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
		}
		//Counting
		for(i=2;i<=5;i++){
			if(pieces[i]>0){
				total++;
				if(pieces[i]==turn)
					numSelf++;
			}
		}		
		for(i=10;i<13;i++){
			if(pieces[i]>0){
				total++;
				if(pieces[i]==turn)
					numSelf++;
			}
		}
		for(i=16;i<47;i++){
			if(pieces[i]>0){
				total++;
				if(pieces[i]==turn)
					numSelf++;
			}
		}
		for(i=50;i<53;i++){
			if(pieces[i]>0){
				total++;
				if(pieces[i]==turn)
					numSelf++;
			}
		}
		for(i=58;i<61;i++){
			if(pieces[i]>0){
				total++;
				if(pieces[i]==turn)
					numSelf++;
			}
		}
		
		return (int) Math.round(score+moves[0]*AgentValues.oppVal3+(2*numSelf-total)*AgentValues.pieceVal3);
	}
	
	public static int e1(int[] pieces, int state, int[] moves,int turn,int other){
		if(state>1)
			if(state==turn+1)
				return AgentValues.goalVal;
			else
				return -AgentValues.goalVal;
		int numSelf=0;
		int i;
		int total=0;
		int score=0;
		//Evaluate Corners.
		if(pieces[0]>0){
			total++;
			if(pieces[0]==turn){
				numSelf++;
				score+=AgentValues.cornerVal;
			}else{
				score-=2*AgentValues.cornerVal;
			}
		}else{
			if(pieces[1]>0){
				total++;
				if(pieces[1]==turn){
					score-=2*AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
			if(pieces[8]>0){
				total++;
				if(pieces[8]==turn){
					score-=2*AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}		
			if(pieces[9]>0){
				total++;
				if(pieces[9]==turn){
					score-=2*AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
		}
		if(pieces[7]>0){
			total++;
			if(pieces[7]==turn){
				score+=AgentValues.cornerVal;
			}else{
				score-=2*AgentValues.cornerVal;
			}
		}else{
			if(pieces[6]>0){
				total++;
				if(pieces[6]==turn){
					score-=2*AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
			if(pieces[14]>0){
				total++;
				if(pieces[14]==turn){
					score-=2*AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
			if(pieces[15]>0){
				total++;
				if(pieces[15]==turn){
					score-=2*AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
		}
		if(pieces[56]>0){
			total++;
			if(pieces[63]==turn){
				score+=AgentValues.cornerVal;
			}else{
				score-=2*AgentValues.cornerVal;
			}
		}else{
			if(pieces[48]>0){
				total++;
				if(pieces[48]==turn){
					score-=2*AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
			if(pieces[49]>0){
				total++;
				if(pieces[49]==turn){
					score-=2*AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
			if(pieces[57]>0){
				total++;
				if(pieces[57]==turn){
					score-=2*AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
		}
		if(pieces[63]>0){
			total++;
			if(pieces[63]==turn){
				score+=AgentValues.cornerVal;
			}else{
				score-=2*AgentValues.cornerVal;
			}
		}else{
			if(pieces[54]>0){
				total++;
				if(pieces[54]==turn){
					score-=2*AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
			if(pieces[55]>0){
				total++;
				if(pieces[55]==turn){
					score-=2*AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
			if(pieces[62]>0){
				total++;
				if(pieces[62]==turn){
					score-=2*AgentValues.nextCornerVal;
				}else{
					score+=AgentValues.nextCornerVal;
				}
			}
		}
		//Counting
		for(i=2;i<=5;i++){
			if(pieces[i]>0){
				total++;
				if(pieces[i]==turn)
					numSelf++;
			}
		}		
		for(i=10;i<13;i++){
			if(pieces[i]>0){
				total++;
				if(pieces[i]==turn)
					numSelf++;
			}
		}
		for(i=16;i<47;i++){
			if(pieces[i]>0){
				total++;
				if(pieces[i]==turn)
					numSelf++;
			}
		}
		for(i=50;i<53;i++){
			if(pieces[i]>0){
				total++;
				if(pieces[i]==turn)
					numSelf++;
			}
		}
		for(i=58;i<61;i++){
			if(pieces[i]>0){
				total++;
				if(pieces[i]==turn)
					numSelf++;
			}
		}
		int reversibility=MIN;
		int bestMove=moves[0];
		int stability=MIN;
		int temp;
		int[] oppMoves=Agent.findLegalMoves(pieces, other);
		int opp=oppMoves[0];
		for(int j=1;j<moves[0];j++){
			temp=GameBoard.numTurned(pieces,moves[j],turn);
			if(temp>reversibility){
				bestMove=moves[j];
				reversibility=temp;
			}
		}
		int[] nextMove=Agent.findLegalMoves(GameBoard.resultFrom(pieces,bestMove,turn),other);
		for(int j=1;j<nextMove[0];j++)
			stability=Math.max(stability,GameBoard.numTurned(pieces,moves[j],other));
		return (int) Math.round(score+(moves[0]-opp)*AgentValues.oppVal3+(2*numSelf-total+reversibility-stability)*AgentValues.pieceVal3*1.5);
	}
}
